import { GetCustomizedPrice } from "@/api";

export const useContent = (props) => {
    const route = useRoute();
    const orderStore = useOrderStore();
    const dialogStore = useDialogStore();
    const userStore = useUserStore();
    const template_id = route.params.id;

    const onFreeExperience = () => {
        navigateTo("/template.html");
    };

    const IsCheckBuyStatus = (code) => {
        const id = props.priceMap[code].id;
        return (
            props.buyList.findIndex(
                (item) =>
                    item.temspecs_id == id && item.temspecs_type == props.unit
            ) != -1
        );
    };

    const onContactCustomerService = async () => {
        if (userStore.isLogin) {
            const res = await GetCustomizedPrice();
            orderStore.order_type = 2;
            orderStore.buy_type = "customer";
            orderStore.temspecs_type = "customer";
            orderStore.authority = ["请咨询客服"];
            orderStore.template_id = template_id;
            orderStore.order_price = res.data;
            dialogStore.isDetailDialogVisible = true;
        } else {
            LoginModal();
        }
    };

    const getUnit = () => {
        return {
            month: "月",
            year: "年",
        }[props.unit];
    };

    const onBuy = (type, authority) => {
        if (userStore.isLogin) {
            // 判断是否已经购买
            if (!IsCheckBuyStatus(type)) {
                orderStore.order_type = 2;
                orderStore.buy_type = type;
                orderStore.temspecs_type = props.unit;
                orderStore.authority = authority;
                orderStore.temspecs_id = props.priceMap[type].id;
                orderStore.template_id = template_id;
                orderStore.order_price = props.priceMap[type].price[props.unit];
                dialogStore.isDetailDialogVisible = true;
            }
        } else {
            LoginModal();
        }
    };

    return {
        onBuy,
        getUnit,
        onFreeExperience,
        IsCheckBuyStatus,
        onContactCustomerService,
    };
};
